<?php

/**
 * Created by 无所谓已
 * User: Administrator
 * Date: 2025-08-26
 * Time: 0:11
 */

namespace plugin\csauto\app\process;

use app\service\StableWorkerIdService;
use plugin\admin\app\model\Rule;
use plugin\csauto\app\model\SiteModel;
use plugin\csauto\app\model\NcServerModel;
use plugin\csauto\app\service\ApiWorkService;
use plugin\csauto\app\service\downloader\TorrentTaskService;
use plugin\csauto\app\service\SiteWorkService;
use plugin\csauto\app\service\SystemService;
use Shopwwi\LaravelCache\Cache;
use Workerman\Timer;
use function Symfony\Component\Clock\now;

/**
 * Class SystemInitProcess
 * 初始化引擎
 *
 * @package plugin\csauto\app\process
 */
class SystemInitProcess
{
    // 定时器句柄/ID
    protected $timer;
    protected $wechatTimer;

    // 时间间隔 (单位: 秒, 默认5秒)
    protected $interval = 900;

    public function onWorkerStart()
    {
        if (getenv('WORK_DEBUG')) return true;
        try {
            // 这里执行系统预设的定时任务事件
            $this->updateSql();
            $this->run();
            
            // 打印系统版本号
            $version = SystemService::getConfig(SystemService::SYSTEM_VERSION);
            echo "========================================" . PHP_EOL;
            echo "系统版本号: " . $version . PHP_EOL;
            
            // 从版本号中提取时间戳并格式化显示
            $versionParts = explode('.', $version);
            if (count($versionParts) >= 4) {
                $timestamp = end($versionParts);
                if (is_numeric($timestamp)) {
                    $updateTime = date('Y-m-d H:i:s', $timestamp);
                    echo "更新时间: " . $updateTime . PHP_EOL;
                }
            }
            echo "========================================" . PHP_EOL;
        } catch (\Throwable $e) {

            echo 'ERROR[SystemInitProcess]: ' . $e->getMessage() . PHP_EOL;
            print_r($e->getTrace());
        }
        $this->timer = Timer::add($this->interval, function () {
            try {
                // 这里执行系统预设的定时任务事件
                $this->run();
            } catch (\Throwable $e) {
                echo 'ERROR[SystemInitProcess]: ' . $e->getMessage() . PHP_EOL;
            }
        });

        return true;
    }

    private function updateSql()
    {
        // 注意：必须先执行字段添加操作，再执行数据更新操作
        $this->updateTorrentTaskMediaInfo();
        $this->updateSiteRulesTable(); // 新增的检查方法
        $this->updateTorrentReleaseApiColumns(); // 新增的检查方法
        $this->updateTorrentTaskRetryCount(); // 新增的MediaInfo重试计数字段
        $this->updateDownloaderNcThrottled(); // 新增的下载器NC限速标记字段
        $this->updateNcServersTable(); // 新增的NC服务器账户表
        $this->updateDownloaderTrafficColumns(); // 新增的下载器流量统计字段
        $this->updateDownloaderTaskSizeRangeColumns(); // 新增的下载器任务种子大小范围字段（GB）
        $this->updateTorrentReleaseRetryCount(); // 新增的发布重试计数字段
        $this->updateSystemReadonly(); // 新增的系统参数只读字段
        // 字段添加完成后，再执行数据更新操作
        SystemService::updateSystemDb();
        $this->addRules();
    }

    private function updateTorrentTaskMediaInfo()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 使用项目默认的数据库模型方式查询
            $result = \support\think\Db::query("
            SELECT COLUMN_TYPE 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_SCHEMA = DATABASE() 
            AND TABLE_NAME = ? 
            AND COLUMN_NAME = ?
        ", ['cs_torrent_task', 'mediaInfo']);

            // 处理数组结果
            $columnInfo = !empty($result) ? $result[0] : null;

            if ($columnInfo && isset($columnInfo['COLUMN_TYPE']) && $columnInfo['COLUMN_TYPE'] === 'text') {
                // 执行升级：修改字段类型为 MEDIUMTEXT
                \support\think\Db::execute("ALTER TABLE `cs_torrent_task` MODIFY `mediaInfo` MEDIUMTEXT");
                echo "字段 `mediaInfo` 已成功升级为 MEDIUMTEXT。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateDownloaderNcThrottled()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_downloader 表中 nc_throttled 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE()
            AND TABLE_NAME = ?
            AND COLUMN_NAME = ?
        ", ['cs_downloader', 'nc_throttled']);

            if (empty($result)) {
                // 添加 nc_throttled 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_downloader`
                ADD COLUMN `nc_throttled` tinyint NULL DEFAULT 0 AFTER `task_limit`
            ");
                echo "字段 `nc_throttled` 已成功添加到表 `cs_downloader`。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateNcServersTable()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_nc_servers 表是否存在
            $result = \support\think\Db::query("
            SELECT TABLE_NAME
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_SCHEMA = DATABASE()
            AND TABLE_NAME = ?
        ", ['cs_nc_servers']);

            if (empty($result)) {
                // 表不存在，需要创建表
                \support\think\Db::execute("
                CREATE TABLE `cs_nc_servers`  (
                  `id` int NOT NULL AUTO_INCREMENT,
                  `customer_number` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'NC客户编号',
                  `scp_password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT 'SCP密码',
                  `server_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '服务器名称',
                  `status` tinyint NULL DEFAULT 1 COMMENT '状态 0-禁用 1-启用',
                  `notes` text CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '备注',
                  `created_at` datetime NULL DEFAULT NULL,
                  `updated_at` datetime NULL DEFAULT NULL,
                  PRIMARY KEY (`id`) USING BTREE,
                  INDEX `status`(`status` ASC) USING BTREE
                ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic AUTO_INCREMENT=1;
            ");
                echo "表 `cs_nc_servers` 已成功创建。\n";
            }
        } catch (\Exception $e) {
            echo "数据库表创建检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateDownloaderTrafficColumns()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 需要添加的字段列表
            $columns = [
                'traffic_in' => "ADD COLUMN `traffic_in` bigint NULL DEFAULT 0 COMMENT '当月入站流量(GB)' AFTER `nc_throttled`",
                'traffic_out' => "ADD COLUMN `traffic_out` bigint NULL DEFAULT 0 COMMENT '当月出站流量(GB)' AFTER `traffic_in`",
                'traffic_total' => "ADD COLUMN `traffic_total` bigint NULL DEFAULT 0 COMMENT '当月总流量(GB)' AFTER `traffic_out`",
                'last_traffic_check_at' => "ADD COLUMN `last_traffic_check_at` datetime NULL DEFAULT NULL COMMENT '最后流量检查时间' AFTER `traffic_total`"
            ];

            foreach ($columns as $columnName => $alterSql) {
                // 检查字段是否存在
                $result = \support\think\Db::query("
                    SELECT COLUMN_NAME
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE TABLE_SCHEMA = DATABASE()
                    AND TABLE_NAME = ?
                    AND COLUMN_NAME = ?
                ", ['cs_downloader', $columnName]);

                if (empty($result)) {
                    // 添加字段
                    \support\think\Db::execute("ALTER TABLE `cs_downloader` {$alterSql}");
                    echo "字段 `{$columnName}` 已成功添加到表 `cs_downloader`。\n";
                }
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateDownloaderTaskSizeRangeColumns()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 需要添加的字段列表（单位：GB）
            $columns = [
                'task_torrent_min_size' => "ADD COLUMN `task_torrent_min_size` int NULL DEFAULT 0 COMMENT '任务种子最小体积(GB)，0为不限制' AFTER `task_size`",
                'task_torrent_max_size' => "ADD COLUMN `task_torrent_max_size` int NULL DEFAULT 0 COMMENT '任务种子最大体积(GB)，0为不限制' AFTER `task_torrent_min_size`",
            ];

            foreach ($columns as $columnName => $alterSql) {
                // 检查字段是否存在
                $result = \support\think\Db::query("
                    SELECT COLUMN_NAME
                    FROM INFORMATION_SCHEMA.COLUMNS
                    WHERE TABLE_SCHEMA = DATABASE()
                    AND TABLE_NAME = ?
                    AND COLUMN_NAME = ?
                ", ['cs_downloader', $columnName]);

                if (empty($result)) {
                    // 添加字段
                    \support\think\Db::execute("ALTER TABLE `cs_downloader` {$alterSql}");
                    echo "字段 `{$columnName}` 已成功添加到表 `cs_downloader`。\n";
                }
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateSiteRulesTable()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_site_rules 表是否存在
            $result = \support\think\Db::query("
            SELECT TABLE_NAME 
            FROM INFORMATION_SCHEMA.TABLES 
            WHERE TABLE_SCHEMA = DATABASE() 
            AND TABLE_NAME = ?
        ", ['cs_site_rules']);

            if (empty($result)) {
                // 表不存在，需要创建表
                \support\think\Db::execute("
                CREATE TABLE `cs_site_rules`  (
                  `id` int NOT NULL AUTO_INCREMENT,
                  `scope_site_id` json NULL COMMENT '生效范围ID 留空则全局',
                  `rule_type` int NULL DEFAULT 0 COMMENT '规则类型',
                  `rule_compare` int NULL DEFAULT NULL COMMENT '规则比较',
                  `rule_value` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '规则参数',
                  `rule_action` tinyint NULL DEFAULT 0,
                  `rule_status` tinyint NULL DEFAULT 0,
                  `sort` smallint NULL DEFAULT 0 COMMENT '排序',
                  `notes` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
                  `is_system` tinyint NULL DEFAULT 0,
                  `created_at` datetime NULL DEFAULT NULL,
                  `updated_at` datetime NULL DEFAULT NULL,
                  PRIMARY KEY (`id`) USING BTREE,
                  INDEX `rule_status`(`rule_status` ASC, `sort` DESC) USING BTREE
                ) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic AUTO_INCREMENT=10000;
            ");
                echo "表 `cs_site_rules` 已成功创建，自增ID从10000开始。\n";
            }
            // 检查 ID 为 10000 的记录是否存在
            $recordResult = \support\think\Db::query("
            SELECT id 
            FROM cs_site_rules 
            WHERE id = ?
        ", [10000]);

            // 如果 ID 为 10000 的记录不存在，则插入默认记录
            if (empty($recordResult)) {
                \support\think\Db::execute("
                INSERT INTO cs_site_rules 
                (id, scope_site_id, rule_type, rule_compare, rule_value, rule_action, rule_status, notes, sort, is_system, created_at, updated_at)
                VALUES 
                (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
            ", [
                    10000,                     // id
                    null,                      // scope_site_id (空值)
                    1,                         // rule_type
                    1,                         // rule_compare
                    '2',                       // rule_value
                    1,                         // rule_action
                    1,                         // rule_status
                    '',                        // notes
                    0,                         // sort
                    1,                         // is_system
                ]);
                echo "默认记录已成功插入到 `cs_site_rules` 表中，ID 为 10000。\n";
            }
        } catch (\Exception $e) {
            echo "数据库表创建检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateTorrentReleaseApiColumns()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_torrent_release 表中 api_id 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_SCHEMA = DATABASE() 
            AND TABLE_NAME = ? 
            AND COLUMN_NAME = ?
        ", ['cs_torrent_release', 'api_id']);

            if (empty($result)) {
                // 添加 api_id 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_torrent_release` 
                ADD COLUMN `api_id` int NULL DEFAULT 0 AFTER `task_status`
            ");
                echo "字段 `api_id` 已成功添加到表 `cs_torrent_release`。\n";
            }
            // 检查 cs_torrent_release 表中 api_status 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME 
            FROM INFORMATION_SCHEMA.COLUMNS 
            WHERE TABLE_SCHEMA = DATABASE() 
            AND TABLE_NAME = ? 
            AND COLUMN_NAME = ?
        ", ['cs_torrent_release', 'api_status']);

            if (empty($result)) {
                // 添加 api_status 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_torrent_release` 
                ADD COLUMN `api_status` tinyint NULL DEFAULT 0 AFTER `api_id`
            ");
                echo "字段 `api_status` 已成功添加到表 `cs_torrent_release`。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateTorrentTaskRetryCount()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_torrent_task 表中 mediainfo_retry_count 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE()
            AND TABLE_NAME = ?
            AND COLUMN_NAME = ?
        ", ['cs_torrent_task', 'mediainfo_retry_count']);

            if (empty($result)) {
                // 添加 mediainfo_retry_count 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_torrent_task`
                ADD COLUMN `mediainfo_retry_count` tinyint NULL DEFAULT 0 AFTER `mediaInfo_status`
            ");
                echo "字段 `mediainfo_retry_count` 已成功添加到表 `cs_torrent_task`。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateTorrentReleaseRetryCount()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_torrent_release 表中 retry_count 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE()
            AND TABLE_NAME = ?
            AND COLUMN_NAME = ?
        ", ['cs_torrent_release', 'retry_count']);

            if (empty($result)) {
                // 添加 retry_count 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_torrent_release`
                ADD COLUMN `retry_count` tinyint NULL DEFAULT 0 AFTER `task_status`
            ");
                echo "字段 `retry_count` 已成功添加到表 `cs_torrent_release`。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function updateSystemReadonly()
    {
        try {
            // 检查 Db 类是否存在
            if (!class_exists('\support\think\Db')) {
                echo "support\\think\\Db 类不存在，跳过数据库更新检查。\n";
                return;
            }

            // 检查 cs_system 表中 readonly 字段是否存在
            $result = \support\think\Db::query("
            SELECT COLUMN_NAME
            FROM INFORMATION_SCHEMA.COLUMNS
            WHERE TABLE_SCHEMA = DATABASE()
            AND TABLE_NAME = ?
            AND COLUMN_NAME = ?
        ", ['cs_system', 'readonly']);

            if (empty($result)) {
                // 添加 readonly 字段
                \support\think\Db::execute("
                ALTER TABLE `cs_system`
                ADD COLUMN `readonly` tinyint NULL DEFAULT 0 AFTER `note`
            ");
                echo "字段 `readonly` 已成功添加到表 `cs_system`。\n";
            }
        } catch (\Exception $e) {
            echo "数据库字段更新检查出错: " . $e->getMessage() . PHP_EOL;
            echo "跳过数据库更新检查。\n";
        }
    }

    private function addRules()
    {
        $ruleList = array(
            array(
                'id' => 118,
                'title' => '系统设置',
                'icon' => '',
                'key' => 'plugin\csauto\system\index',
                'pid' => 114,
                'href' => '/app/csauto/system/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 119,
                'title' => '发布规则',
                'icon' => '',
                'key' => 'plugin\csauto\rules\index',
                'pid' => 114,
                'href' => '/app/csauto/rules/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 120,
                'title' => 'RSS种子列表',
                'icon' => '',
                'key' => 'plugin\csauto\torrentlist\index',
                'pid' => 114,
                'href' => '/app/csauto/torrentlist/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 121,
                'title' => '种子发布任务',
                'icon' => '',
                'key' => 'plugin\csauto\torrentrelease\index',
                'pid' => 114,
                'href' => '/app/csauto/torrentrelease/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 122,
                'title' => '下载任务管理',
                'icon' => '',
                'key' => 'plugin\csauto\torrenttask\index',
                'pid' => 114,
                'href' => '/app/csauto/torrenttask/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 123,
                'title' => '发布统计',
                'icon' => '',
                'key' => 'plugin\csauto\torrentreleasestatistics\index',
                'pid' => 114,
                'href' => '/app/csauto/torrentreleasestatistics/index',
                'type' => 1,
                'weight' => 0
            ),
            array(
                'id' => 124,
                'title' => 'NC服务器管理',
                'icon' => '',
                'key' => 'plugin\csauto\ncservers\index',
                'pid' => 114,
                'href' => '/app/csauto/ncservers/index',
                'type' => 1,
                'weight' => 0
            )
        );

        foreach ($ruleList as $item) {
            $existingRule = Rule::query()->where('id', $item['id'])->first();

            if (!$existingRule) {
                // 记录不存在，插入新记录
                $item['created_at'] = now();
                $item['updated_at'] = now();
                Rule::query()->insert($item);
                echo "Rule add >>>>> {$item['id']}" . PHP_EOL;
            } else {
                // 记录存在，检查 href 是否正确
                if (
                    $existingRule->href != $item['href'] ||
                    $existingRule->title != $item['title'] ||
                    $existingRule->key != $item['key'] ||
                    $existingRule->pid != $item['pid'] ||
                    $existingRule->type != $item['type'] ||
                    $existingRule->weight != $item['weight']
                ) {

                    // href 或其他字段不正确，更新记录
                    $updateData = $item;
                    $updateData['updated_at'] = now();
                    unset($updateData['id']); // 移除ID字段，避免更新ID
                    unset($updateData['created_at']); // 移除created_at字段

                    Rule::query()->where('id', $item['id'])->update($updateData);
                    echo "Rule update >>>>> {$item['id']}" . PHP_EOL;
                } else {
                    echo "Rule exists and is correct >>>>> {$item['id']}" . PHP_EOL;
                }
            }
        }
    }

    public function run()
    {
        addlog('SystemInitProcess', 'start');
        $this->checkAutoKey();
        $apiWorkData = ApiWorkService::SiteList();
        //        print_r($apiWorkData) . PHP_EOL;
        if ($apiWorkData) {
            SiteWorkService::updateSite($apiWorkData);
        }
        return '任务调用：' . date('Y-m-d H:i:s') . "\n";
    }

    private function checkAutoKey()
    {
        $siteUid = getenv('SITE_UID');
        $siteToolKey = getenv('SITE_TOOL_KEY');

        $userToolToken = Cache::store('file')->get(SystemService::USER_TOOL_TOKEN);
        /*校验操作或者刷新KEY*/
        $getData = array(
            'site_id' => 2,
            'site_uid' => $siteUid,
            'site_tool_key' => $siteToolKey,
            'device_id' => StableWorkerIdService::getStableWorkerId()
        );
        if (!empty($userToolToken)) {
            $getData['token'] = $userToolToken;
        }
        $apiTokenData = ApiWorkService::updateAutoToken($getData);
        if (!empty($apiTokenData['tokenKey'])) {
            Cache::store('file')->put(SystemService::USER_TOOL_TOKEN, $apiTokenData['tokenKey'], strtotime($apiTokenData['expire_time']) - time());
            echo "getToken：{$apiTokenData['tokenKey']}" . PHP_EOL;
        }
        echo "Time：" . (strtotime($apiTokenData['expire_time']) - time()) . PHP_EOL;
        echo 'Token：' . Cache::store('file')->get(SystemService::USER_TOOL_TOKEN) . PHP_EOL;
        echo "初始化成功" . PHP_EOL;
    }

    /**
     * 停止/删除定时器
     *
     * @return bool
     */
    public function stop()
    {
        return Timer::del($this->timer);
    }
}
